package com.treasure.mall.zbqmallmanager.controller.order;

import com.treasure.mall.biz.dto.condition.AfterSaleCondition;
import com.treasure.mall.biz.entity.order.AfterSaleBillPO;
import com.treasure.mall.biz.service.core.order.AfterSaleService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import com.treasure.mall.zbq.common.ienum.order.OrderEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/12
 */
@Controller
@RequestMapping("/order/afterSale")
public class AfterSaleController {
    @Autowired
    private AfterSaleService afterSaleService;

    @GetMapping("/list")
    public String list(
            @ModelAttribute("condition") AfterSaleCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<AfterSaleBillPO> orderPOS = afterSaleService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);

        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", orderPOS.getTotalElements());
        model.addAttribute("totalPages", orderPOS.getTotalPages());
        model.addAttribute("bills", orderPOS.getContent());
        model.addAttribute("statuses", OrderEnums.AfterSaleStatus.values());

        return "order/after_sale_list";
    }

    @GetMapping("/detail")
    public String detail(String id, Model model) {
        AfterSaleBillPO afterSaleBillPO = afterSaleService.findById(id);
        model.addAttribute("bill", afterSaleBillPO);

        return "order/after_sale_detail";
    }

    @PostMapping("/approve")
    @ResponseBody
    public ApiResult approve(String id) {
        afterSaleService.approve(id);
        return ApiResult.ok();
    }

    @PostMapping("/refuse")
    @ResponseBody
    public ApiResult refuse(String id, String remark) {
        afterSaleService.refuse(id, remark);
        return ApiResult.ok();
    }

    @PostMapping("/refund")
    @ResponseBody
    public ApiResult refund(String id) {
        afterSaleService.refund(id, OrderEnums.RefundType.ORIGINAL_WAY.getCode());
        return ApiResult.ok();
    }
}
